package com.github.satant.job.common.biz.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.satant.job.common.biz.BaseBiz;
import com.github.satant.job.common.msg.TableResultResponse;
import com.github.satant.job.common.util.Query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * @auther: dear_satant
 * @Description： 业务基类
 * @date: 2018.6.26 21:23
 */
@Transactional(rollbackFor = Exception.class)
public abstract class BaseBizImpl<M extends Mapper<T>, T> implements BaseBiz<M,T> {
    @Autowired
    protected M mapper;

    @Override
    public void setMapper(M mapper) {
        this.mapper = mapper;
    }
    @Override
    public T selectOne(T entity) {
        return mapper.selectOne(entity);
    }

    @Override
    public T selectById(Object id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<T> selectList(T entity) {
        return mapper.select(entity);
    }

    @Override
    public List<T> selectListAll() {
        return mapper.selectAll();
    }

    @Override
    public Long selectCount(T entity) {
        return new Long(mapper.selectCount(entity));
    }

    @Override
    public void insert(T entity) {
        mapper.insert(entity);
    }

    @Override
    public T insertSelective(T entity) {
        mapper.insertSelective(entity);
        return entity;
    }

    @Override
    public void delete(T entity) {
        mapper.delete(entity);
    }

    @Override
    public void deleteById(Object id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateById(T entity) {
        mapper.updateByPrimaryKey(entity);
    }

    @Override
    public void updateSelectiveById(T entity) {
        mapper.updateByPrimaryKeySelective(entity);

    }

    @Override
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    @Override
    public int selectCountByExample(Object example) {
        return mapper.selectCountByExample(example);
    }

    @Override
    public TableResultResponse<T> selectByQuery(Query query) {
        return selectByQueryWithOrder(query,"0","0");
    }

    /**
     *
     * @param query 查询参数
     * @param orderField 排序字段 当此字段为0时，不做排序
     * @param orderType 排序方式
     * @return
     */
    @Override
    public TableResultResponse<T> selectByQueryWithOrder(Query query, String orderField,
                                                         String orderType) {
        Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        Example example = new Example(clazz);
        Example.Criteria criteria = example.createCriteria();
        for (Map.Entry<String, Object> entry : query.entrySet()) {
            criteria.andLike(entry.getKey(), "%" + entry.getValue().toString() + "%");
        }
        if(!StringUtils.equals(orderField,"0")){
            addOrderCondition(example,orderField,orderType);
        }
        Page<Object> result = PageHelper.startPage(query.getPage(), query.getLimit());
        List<T> list = mapper.selectByExample(example);
        return new TableResultResponse<T>(result.getTotal(), list);
    }



    private Example addOrderCondition(Example example,String orderField,String orderType) throws IllegalArgumentException{
        String[] fields = StringUtils.split(orderField, ",");
        String[] orders = StringUtils.split(orderType, ",");
        if(fields.length != orders.length){
            throw new IllegalArgumentException("orderField必须和orderType相等!");
        }
        for(int i =0;i<fields.length;i++){
            if(StringUtils.equals(orders[i],"a")){
                example.orderBy(fields[i]).asc();
            }else{
                example.orderBy(fields[i]).desc();
            }
        }
        return example;
    }
}